package service

import (
	"gitee.com/llb1020/k8s-deme/controller"
	"gitee.com/llb1020/k8s-deme/initk8s"
	meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/fields"
	"k8s.io/client-go/tools/cache"
	"k8s.io/client-go/util/workqueue"
	"k8s.io/klog/v2"
	v1 "k8s.io/api/core/v1"
)

func GetPodList()  {
	clientset, err := initk8s.InitClient()
	if err != nil {
		klog.Fatal(err)
	}
	// create the pod watcher
	podListWatcher := cache.NewListWatchFromClient(clientset.CoreV1().RESTClient(), "pods", v1.NamespaceAll, fields.Everything())

	// create the workqueue
	queue := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())

	indexer, informer := cache.NewIndexerInformer(podListWatcher, &v1.Pod{}, 0, cache.ResourceEventHandlerFuncs{}, cache.Indexers{})

	controller := controller.Controller{
		Indexer: indexer,
		Informer: informer,
		Queue: queue,
	}

	indexer.Add(&v1.Pod{
		ObjectMeta: meta_v1.ObjectMeta{
			Name:      "",
			Namespace: v1.NamespaceAll,
		},
	})

	// Now let's start the controller
	stop := make(chan struct{})
	defer close(stop)
	go controller.Run(1, stop)
	// Wait forever
	select {}
}
